<?php

/**
 * Created by PhpStorm.
 * User: kc
 * Date: 2017/3/3
 * Time: 14:58
 */
class AhpModuleController extends BaseController{
    /**
     * @Author:      kc
     * @DateTime:   2017-03-03 15:00:00
     * @Description: 新建模块
     */
    public function createModule(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'mod_number' => 'required|unique:ahp_module_base,mod_number',
                'mod_name' => 'required|unique:ahp_module_base,mod_name',
                'mod_domain' => 'required',
                'mod_type' => 'required'
            )
        );
        do{
            $messages = $validator->messages();
            //验证模板编号唯一
            if($messages->has('mod_number')) {
                $response = Lang::get('ahp_error.ahp_module_base_unique_error_4100');
                break;
            }
            //验证模板名称唯一
            if($messages->has('mod_name')){
                $response = Lang::get('ahp_error.ahp_module_name_unique_error_4101');
                break;
            }
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            try{
                $module = new AhpModuleBase();
                $module->mod_number = Input::get('mod_number');
                $module->mod_name = Input::get('mod_name');
                if(Input::get('mod_img')){
                    $img = base64_decode(Input::get('mod_img'));
                    $pic_name = date("YmdHis", time()) . rand(10000, 99999) . '.png';
                    $path = Config::get('config.system_img');
                    File::put( $path . $pic_name, $img);
                    $module->mod_img = $pic_name;
                }else{
                    $module->mod_img = '';
                }
                $module->mod_url = Input::get('mod_url','');
                $module->mod_domain = Input::get('mod_domain');
                $module->mod_type = Input::get('mod_type');
                $module->mod_show = 1;
                $module->status = 1;
                $module->created_by = $account_user->id;
                $module->save();
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "createModule", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.ahp_create_error_1003");
                break;
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "createModule", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-03 15:59:00
     * @Description: 修改模块
     */
    public function updateModule(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'module_id' => 'required',
                'mod_number' => 'unique:ahp_module_base,mod_number,'.Input::get('module_id'),
                'mod_name' => 'unique:ahp_module_base,mod_name,'.Input::get('module_id'),
            )
        );
        do{
            $messages = $validator->messages();
            //验证用户名唯一
            if($messages->has('mod_number')) {
                $response = Lang::get('ahp_error.ahp_module_base_unique_error_4100');
                break;
            }
            //验证模板名称唯一
            if($messages->has('mod_name')){
                $response = Lang::get('ahp_error.ahp_module_name_unique_error_4101');
                break;
            }
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            try{
                $module = AhpModuleBase::find(Input::get('module_id'));
                $module->mod_number = Input::get('mod_number')?Input::get('mod_number'):'';
                $module->mod_name = Input::get('mod_name')?Input::get('mod_name'):'';
                $module_img = Input::get('mod_img')?Input::get('mod_img'):'';
                if(Input::get('mod_img')){
                    if($module->mod_img == $module_img){
                        $pic_name = $module_img;
                    }else {
                        $img = base64_decode(Input::get('mod_img'));
                        $pic_name = date("YmdHis", time()) . rand(10000, 99999) . '.png';
                        $path = Config::get('config.system_img');
                        File::put($path . $pic_name, $img);
                    }
                    $module->mod_img = $pic_name;
                }else{
                    $module->mod_img =  $module_img;
                }
                $module->mod_url = Input::get('mod_url')?Input::get('mod_url'):'';
                $module->mod_domain = Input::get('mod_domain')?Input::get('mod_domain'):'';
                $module->mod_type = Input::get('mod_type')?Input::get('mod_type'):'';
                $module->created_by = $account_user->id;
                $module->updated_at = Common::currentTimeString();
                $module->save();
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "updateModule", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.ahp_update_error_1004");
                break;
            }
            $response = array(
                "code" => 0,
                "msg" => "ok",
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "updateModule", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-03 17:19:24
     * @Description: 模块列表
     */
    public function moduleList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $page = Input::get('page',1);
            $page_size = Input::get('page_size',10);
            $ahp_module = AhpModuleBase::getModule(Input::get('mod_number'),Input::get('mod_name'),Input::get('start_time'),Input::get('end_time'),Input::get('type'),$page,$page_size);
            $response = array(
                "code" => 0,
                "msg" => "ok",
                "result" => $ahp_module,
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "moduleList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-03 17:19:24
     * @Description: 修改模块状态
     */
    public function updateModuleStatus(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'module_id' => 'required',
                'status' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $ids = explode(',',Input::get('module_id'));
            try{
                AhpModuleBase::whereIn('id',$ids)->update(['status' => Input::get('status'),'created_at' => Common::currentTimeString()]);
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "updateModuleStatus", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.ahp_update_status_error_1005");
                break;
            }
            $response = array(
                'code' => 0,
                'msg' => "ok",
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "updateModuleStatus", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-06 14:55:50
     * @Description: 模块详情
     */
    public function moduleDetails(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'module_id' => 'required'
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $module_details = AhpModuleBase::getModuleDetails(Input::get('module_id'));
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $module_details,
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "moduleDetails", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-06 17:39:30
     * @Description: 模块类型列表
     */
    public function moduleType(){
        $module_type = array();
        foreach(AhpModuleBase::$module_type as $key => $value){
            $module_type[] = array(
              'type_id' => $key,
              'type_name' => $value,
            );
        }
        $response = array(
            'code' => 0,
            'msg' => 'ok',
            'result' => $module_type,
        );
        Log::info("info", array("system" => "ahp" ,"event" => "moduleType", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}